var platform_aggregate_map = function(){ 
    var key = {
        gnss_centerid:NumberInt(this.gnss_centerid),
        owner_unit : this.owner_unit,
        platform_name:this.platform_name
    };
    var values = {
	v_id : this.v_id,
	v_has_net_id : this.v_has_net_id,
	v_has_track_id : this.v_has_track_id,
	v_alarm_id : this.v_alarm_id,
	v_location_id : this.v_location_id,
	v_exception_id : this.v_exception_id,
	enterprise_ids : this.enterprise_ids,
	trade_kind_id:this.trade_kind_id,
	has_net : this.has_net,
	has_track : this.has_track,
	has_alarm : this.has_alarm,
	alarm_count_list : this.alarm_count_list,
    alarm_processed_list : this.alarm_processed_list,
    overspeed_count_list : this.overspeed_count_list,
    overspeed_processed_list : this.overspeed_processed_list,
    tired_count_list : this.tired_count_list,
    tired_processed_list : this.tired_processed_list,
    score                       : this.score                       ,  
    enterprise_count            : this.enterprise_count            ,
    disconnect_count                 : this.disconnect_count                 ,
    online_time                 : this.online_time                 ,
    online_rate         : this.online_rate         ,
    total_time                  : 86400                 ,
    vehicle_count               : this.vehicle_count               ,
    vehicle_access_count        : this.vehicle_access_count        ,
    vehicle_online_count        : this.vehicle_online_count        ,
    vehicle_online_rate         : this.vehicle_online_rate         ,
    vehicle_access_rate         : this.vehicle_access_rate         ,
    data_correct_rate           : this.data_correct_rate           ,
    data_fault_rate             : this.data_fault_rate             ,
    location_exception_rate     : this.location_exception_rate     ,
    track_normal                : this.track_normal                ,
    alarm_vehicle_count         : this.alarm_vehicle_count         ,
    alarm_count                 : this.alarm_count                 ,
    alarm_processed             : this.alarm_processed             ,
    overspeed_count             : this.overspeed_count             ,
    overspeed_processed         : this.overspeed_processed         ,
    roadoffset_count            : this.roadoffset_count            ,
    roadoffset_processed        : this.roadoffset_processed        ,
    tired_count                 : this.tired_count                 ,
    tired_processed             : this.tired_processed             ,
    tired_duration              : this.tired_duration              ,
    prohibit_driving_count      : this.prohibit_driving_count      ,
    prohibit_driving_processed  : this.prohibit_driving_processed  ,
    nightly_3rd_count           : this.nightly_3rd_count           ,
    nightly_3rd_processed       : this.nightly_3rd_processed       ,
    over_load_count             : this.over_load_count             ,
    over_load_processed         : this.over_load_processed         ,
    overspeed_100_count         : this.overspeed_100_count         ,
    speed_160_count             : this.speed_160_count             ,
    track_complete_rate         : this.track_complete_rate         ,
    exception_track_count       : this.exception_track_count       ,
    gpstime_exception_count     : this.gpstime_exception_count     ,
    location_exception_count    : this.location_exception_count    ,
    speed_exception_count       : this.speed_exception_count       ,
    head_exception_count        : this.head_exception_count        ,
    avg_vehicle_tired_duration  : this.avg_vehicle_tired_duration  ,
    avg_vehicle_speed_counts    : this.avg_vehicle_speed_counts    ,
    exception_vehicle_count     : this.exception_vehicle_count     ,
    vehicle_alarm_rate          : this.vehicle_alarm_rate          ,
    count                       : 1                                
    };
    emit(key, values);
}; 
var platform_aggregate_reduce = function(key, values){ 
    const reduce_result = {
        score                       : 0 ,  
        enterprise_count            : 0 ,
        disconnect_count                 : 0 ,
        online_time                 : 0 ,
        online_rate         : 0 ,
        total_time                  : 0 ,
        vehicle_count               : 0 ,
        vehicle_access_count        : 0 ,
        vehicle_online_count        : 0 ,
        vehicle_online_rate         : 0 ,
        vehicle_access_rate         : 0 ,
        data_correct_rate           : 0 ,
        data_fault_rate             : 0 ,
        location_exception_rate     : 0 ,
        track_normal                : 0 ,
        alarm_vehicle_count         : 0 ,
        alarm_count                 : 0 ,
        alarm_processed             : 0 ,
        overspeed_count             : 0 ,
        overspeed_processed         : 0 ,
        roadoffset_count            : 0 ,
        roadoffset_processed        : 0 ,
        tired_count                 : 0 ,
        tired_processed             : 0 ,
        tired_duration              : 0 ,
        prohibit_driving_count      : 0 ,
        prohibit_driving_processed  : 0 ,
        nightly_3rd_count           : 0 ,
        nightly_3rd_processed       : 0 ,
        over_load_count             : 0 ,
        over_load_processed         : 0 ,
        overspeed_100_count         : 0 ,
        speed_160_count             : 0 ,
        track_complete_rate         : 0 ,
        exception_track_count       : 0 ,
        gpstime_exception_count     : 0 ,
        location_exception_count    : 0 ,
        speed_exception_count       : 0 ,
        head_exception_count        : 0 ,
        avg_vehicle_tired_duration  : 0 ,
        avg_vehicle_speed_counts    : 0 ,
        exception_vehicle_count     : 0 ,
        vehicle_alarm_rate          : 0 ,
        count                       : 0 ,
		enterprise_ids              : [] ,
		v_id:[],
		v_has_net_id : [],
		v_has_track_id : [],
		v_alarm_id : [],
		v_location_id : [],
		v_exception_id : [],
		trade_kind_id:[],
		has_net:[],
		has_track:[],
		has_alarm:[],
		alarm_count_list : [],
        alarm_processed_list : [],
        overspeed_count_list : [],
        overspeed_processed_list : [],
        tired_count_list : [],
        tired_processed_list : [],
    };

    values.forEach(
        function(val){
			reduce_result.trade_kind_id=reduce_result.trade_kind_id.concat(val.trade_kind_id);
			reduce_result.has_net=reduce_result.has_net.concat(val.has_net);
			reduce_result.has_track=reduce_result.has_track.concat(val.has_track);
			reduce_result.has_alarm=reduce_result.has_alarm.concat(val.has_alarm);
			reduce_result.alarm_count_list=reduce_result.alarm_count_list.concat(val.alarm_count_list);
			reduce_result.alarm_processed_list=reduce_result.alarm_processed_list.concat(val.alarm_processed_list);
			reduce_result.overspeed_count_list=reduce_result.overspeed_count_list.concat(val.overspeed_count_list);
			reduce_result.overspeed_processed_list=reduce_result.overspeed_processed_list.concat(val.overspeed_processed_list);
			reduce_result.tired_count_list=reduce_result.tired_count_list.concat(val.tired_count_list);
			reduce_result.tired_processed_list=reduce_result.tired_processed_list.concat(val.tired_processed_list);
			reduce_result.enterprise_ids=reduce_result.enterprise_ids.concat(val.enterprise_ids);
			reduce_result.v_id=reduce_result.v_id.concat(val.v_id);
			reduce_result.v_has_net_id=reduce_result.v_has_net_id.concat(val.v_has_net_id);
			reduce_result.v_has_track_id=reduce_result.v_has_track_id.concat(val.v_has_track_id);
			reduce_result.v_alarm_id=reduce_result.v_alarm_id.concat(val.v_alarm_id);
			reduce_result.v_location_id=reduce_result.v_location_id.concat(val.v_location_id);
			reduce_result.v_exception_id=reduce_result.v_exception_id.concat(val.v_exception_id);
			reduce_result.score                       += val.score                        
            reduce_result.enterprise_count            += val.enterprise_count           
            reduce_result.disconnect_count                 += val.disconnect_count                
            reduce_result.online_time                 += val.online_time                
            reduce_result.online_rate         += val.online_rate        
            reduce_result.total_time                  += val.total_time                 
            reduce_result.vehicle_count               += val.vehicle_count              
            reduce_result.vehicle_access_count        += val.vehicle_access_count       
            reduce_result.vehicle_online_count        += val.vehicle_online_count       
            reduce_result.vehicle_online_rate         += val.vehicle_online_rate        
            reduce_result.vehicle_access_rate         += val.vehicle_access_rate        
            reduce_result.data_correct_rate           += val.data_correct_rate          
            reduce_result.data_fault_rate             += val.data_fault_rate            
            reduce_result.location_exception_rate     += val.location_exception_rate    
            reduce_result.track_normal                += val.track_normal               
            reduce_result.alarm_vehicle_count         += val.alarm_vehicle_count        
            reduce_result.alarm_count                 += val.alarm_count                
            reduce_result.alarm_processed             += val.alarm_processed            
            reduce_result.overspeed_count             += val.overspeed_count            
            reduce_result.overspeed_processed         += val.overspeed_processed        
            reduce_result.roadoffset_count            += val.roadoffset_count           
            reduce_result.roadoffset_processed        += val.roadoffset_processed       
            reduce_result.tired_count                 += val.tired_count                
            reduce_result.tired_processed             += val.tired_processed            
            reduce_result.tired_duration              += val.tired_duration             
            reduce_result.prohibit_driving_count      += val.prohibit_driving_count     
            reduce_result.prohibit_driving_processed  += val.prohibit_driving_processed 
            reduce_result.nightly_3rd_count           += val.nightly_3rd_count          
            reduce_result.nightly_3rd_processed       += val.nightly_3rd_processed      
            reduce_result.over_load_count             += val.over_load_count            
            reduce_result.over_load_processed         += val.over_load_processed        
            reduce_result.overspeed_100_count         += val.overspeed_100_count        
            reduce_result.speed_160_count             += val.speed_160_count            
            reduce_result.track_complete_rate         += val.track_complete_rate        
            reduce_result.exception_track_count       += val.exception_track_count      
            reduce_result.gpstime_exception_count     += val.gpstime_exception_count    
            reduce_result.location_exception_count    += val.location_exception_count   
            reduce_result.speed_exception_count       += val.speed_exception_count      
            reduce_result.head_exception_count        += val.head_exception_count       
            reduce_result.avg_vehicle_tired_duration  += val.avg_vehicle_tired_duration 
            reduce_result.avg_vehicle_speed_counts    += val.avg_vehicle_speed_counts   
            reduce_result.exception_vehicle_count     += val.exception_vehicle_count    
            reduce_result.vehicle_alarm_rate          += val.vehicle_alarm_rate         
            reduce_result.count                       += val.count                      
        }
    ); 

    return reduce_result; 
}; 

var platform_aggregate_finalize= function(key, values){
	var v_id_res = [];
	var v_id_json = {};
	var v_has_net_id_res = [];
	var v_has_net_id_json = {};
	var v_has_track_id_res = [];
	var v_has_track_id_json = {};
	var v_alarm_id_res = [];
	var v_alarm_id_json = {};
	var v_location_id_res = [];
	var v_location_id_json = {};
	var v_exception_id_res = [];
	var v_exception_id_json = {};
	var enterprise_ids_res = [];
	var enterprise_ids_json = {};
	var v_trade_id_json = {};
	var v_trade_has_net_id_json = {};
	var v_trade_has_track_id_json = {};
	var v_trade_alarm_id_json = {};
	var trade_obj={
        1:{trade_name:'班线客运',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        2:{trade_name:'旅游客运',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        3:{trade_name:'危品运输',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        4:{trade_name:'公交车',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        5:{trade_name:'出租车',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        6:{trade_name:'其他',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0}
    }; 
	
	for (var key in trade_obj) {
		trade_obj[key]['vehicle_count']=NumberInt(0);
		trade_obj[key]['vehicle_access_count']=NumberInt(0);
		trade_obj[key]['vehicle_online_count']=NumberInt(0);
		trade_obj[key]['alarm_vehicle_count']=NumberInt(0);
		trade_obj[key]['alarm_count']=NumberInt(0);
		trade_obj[key]['alarm_processed']=NumberInt(0);
		trade_obj[key]['overspeed_count']=NumberInt(0);
		trade_obj[key]['overspeed_processed']=NumberInt(0);
		trade_obj[key]['tired_count']=NumberInt(0);
		trade_obj[key]['tired_processed']=NumberInt(0);
	}
	for(var i in values.v_id){
		if(values.enterprise_ids[i]!=0&&!enterprise_ids_json[values.enterprise_ids[i]]){
		    enterprise_ids_res.push(values.enterprise_ids[i]);
		    enterprise_ids_json[values.enterprise_ids[i]] = 1;
		}
		if(values.v_id[i]!=0&&!v_id_json[values.v_id[i]]){
		    v_id_res.push(values.v_id[i]);
		    v_id_json[values.v_id[i]] = 1;
			if(trade_obj[values.trade_kind_id[i]]){
			    trade_obj[values.trade_kind_id[i]].vehicle_count+=1;
			    trade_obj[values.trade_kind_id[i]].vehicle_count=NumberInt(trade_obj[values.trade_kind_id[i]].vehicle_count);
		    }
		}
		if(values.v_has_net_id[i]!=0&&!v_has_net_id_json[values.v_has_net_id[i]]){
		    v_has_net_id_res.push(values.v_has_net_id[i]);
		    v_has_net_id_json[values.v_has_net_id[i]] = 1;
			if(trade_obj[values.trade_kind_id[i]]){
			    trade_obj[values.trade_kind_id[i]].vehicle_access_count+=1;
				trade_obj[values.trade_kind_id[i]].vehicle_access_count=NumberInt(trade_obj[values.trade_kind_id[i]].vehicle_access_count);
		    }
		}
		if(values.v_has_track_id[i]!=0&&!v_has_track_id_json[values.v_has_track_id[i]]){
		    v_has_track_id_res.push(values.v_has_track_id[i]);
		    v_has_track_id_json[values.v_has_track_id[i]] = 1;
			if(trade_obj[values.trade_kind_id[i]]){
			    trade_obj[values.trade_kind_id[i]].vehicle_online_count+=1;
				trade_obj[values.trade_kind_id[i]].vehicle_online_count=NumberInt(trade_obj[values.trade_kind_id[i]].vehicle_online_count);
		    }
		}
		if(values.v_alarm_id[i]!=0&&!v_alarm_id_json[values.v_alarm_id[i]]){
		    v_alarm_id_res.push(values.v_alarm_id[i]);
		    v_alarm_id_json[values.v_alarm_id[i]] = 1;
			if(trade_obj[values.trade_kind_id[i]]){
			    trade_obj[values.trade_kind_id[i]].alarm_vehicle_count+=1;
				trade_obj[values.trade_kind_id[i]].alarm_vehicle_count=NumberInt(trade_obj[values.trade_kind_id[i]].alarm_vehicle_count);
		    }
		}
		if(values.v_location_id[i]!=0&&!v_location_id_json[values.v_location_id[i]]){
		    v_location_id_res.push(values.v_location_id[i]);
		    v_location_id_json[values.v_location_id[i]] = 1;
		   }
		if(values.v_exception_id[i]!=0&&!v_exception_id_json[values.v_exception_id[i]]){
		    v_exception_id_res.push(values.v_exception_id[i]);
		    v_exception_id_json[values.v_exception_id[i]] = 1;
		   }

		if(trade_obj[values.trade_kind_id[i]]){
			trade_obj[values.trade_kind_id[i]].alarm_count+=values.alarm_count_list[i];
			trade_obj[values.trade_kind_id[i]].alarm_count=NumberInt(trade_obj[values.trade_kind_id[i]].alarm_count);
			trade_obj[values.trade_kind_id[i]].alarm_processed+=values.alarm_processed_list[i];
			trade_obj[values.trade_kind_id[i]].alarm_processed=NumberInt(trade_obj[values.trade_kind_id[i]].alarm_processed);
			trade_obj[values.trade_kind_id[i]].overspeed_count+=values.overspeed_count_list[i];
			trade_obj[values.trade_kind_id[i]].overspeed_count=NumberInt(trade_obj[values.trade_kind_id[i]].overspeed_count);
			trade_obj[values.trade_kind_id[i]].overspeed_processed+=values.overspeed_processed_list[i];
			trade_obj[values.trade_kind_id[i]].overspeed_processed=NumberInt(trade_obj[values.trade_kind_id[i]].overspeed_processed);
			trade_obj[values.trade_kind_id[i]].tired_count+=values.tired_count_list[i];
			trade_obj[values.trade_kind_id[i]].tired_count=NumberInt(trade_obj[values.trade_kind_id[i]].tired_count);
			trade_obj[values.trade_kind_id[i]].tired_processed+=values.tired_processed_list[i];
			trade_obj[values.trade_kind_id[i]].tired_processed=NumberInt(trade_obj[values.trade_kind_id[i]].tired_processed);
			
			if(trade_obj[values.trade_kind_id[i]].vehicle_count>0){
				trade_obj[values.trade_kind_id[i]].vehicle_access_rate=parseFloat((trade_obj[values.trade_kind_id[i]].vehicle_access_count/trade_obj[values.trade_kind_id[i]].vehicle_count*100).toFixed(2));
			}
			if(trade_obj[values.trade_kind_id[i]].vehicle_access_count>0){
				trade_obj[values.trade_kind_id[i]].vehicle_online_rate=parseFloat((trade_obj[values.trade_kind_id[i]].vehicle_online_count/trade_obj[values.trade_kind_id[i]].vehicle_access_count*100).toFixed(2));
			}
			if(trade_obj[values.trade_kind_id[i]].vehicle_online_count>0){
				trade_obj[values.trade_kind_id[i]].alarm_vehicle_rate=parseFloat((trade_obj[values.trade_kind_id[i]].alarm_vehicle_count/trade_obj[values.trade_kind_id[i]].vehicle_online_count*100).toFixed(2));
				trade_obj[values.trade_kind_id[i]].avg_overspeed_count=parseFloat((trade_obj[values.trade_kind_id[i]].overspeed_count/trade_obj[values.trade_kind_id[i]].vehicle_online_count).toFixed(2));
				trade_obj[values.trade_kind_id[i]].avg_tired_count=parseFloat((trade_obj[values.trade_kind_id[i]].tired_count/trade_obj[values.trade_kind_id[i]].vehicle_online_count).toFixed(2));
			}
			if(trade_obj[values.trade_kind_id[i]].alarm_count>0){
				trade_obj[values.trade_kind_id[i]].alarm_processed_rate=parseFloat((trade_obj[values.trade_kind_id[i]].alarm_processed/trade_obj[values.trade_kind_id[i]].alarm_count).toFixed(2));
			}   
		}	
	}
	values.location_exception_vehicle_count=NumberInt(v_location_id_res.length);
	values.vehicle_online_count=NumberInt(v_has_track_id_res.length);
	values.vehicle_count=NumberInt(v_id_res.length);
	values.vehicle_access_count=NumberInt(v_has_net_id_res.length);
	values.alarm_vehicle_count=NumberInt(v_alarm_id_res.length);
	values.exception_vehicle_count=NumberInt(v_exception_id_res.length);
	values.enterprise_count = NumberInt(enterprise_ids_res.length);
    if(values.track_normal>0){
		values.data_correct_rate = parseFloat(((values.track_normal/(values.track_normal+values.exception_track_count))* 100).toFixed(2));
        values.data_fault_rate = parseFloat(((100*100-NumberInt(values.data_correct_rate*100))/100).toFixed(2));
    }else{
		values.data_correct_rate=0.0;
		values.data_fault_rate=0.0;
	}
	if(values.vehicle_online_count>0){
		values.location_exception_rate=parseFloat(((values.location_exception_vehicle_count/values.vehicle_online_count) * 100).toFixed(2));
		values.track_complete_rate=parseFloat(((values.track_complete_rate/values.vehicle_online_count)).toFixed(2));
		values.avg_vehicle_speed_counts=parseFloat((values.overspeed_100_count/values.vehicle_online_count).toFixed(2));
		values.avg_vehicle_tired_duration=parseFloat((values.tired_duration/values.vehicle_online_count).toFixed(2));
	}else{
		values.location_exception_rate=0.0;
		values.track_complete_rate=0.0;
		values.avg_vehicle_speed_counts=0.0;
		values.avg_vehicle_tired_duration=0.0;
	}
	
	if(values.vehicle_count>0){
		values.vehicle_access_rate=parseFloat(((values.vehicle_access_count/values.vehicle_count)* 100).toFixed(2));
	}else{
		values.vehicle_access_rate=0.0;
	}
	
	if(values.vehicle_access_count>0){
		values.vehicle_online_rate=parseFloat(((values.vehicle_online_count/values.vehicle_access_count)* 100).toFixed(2));
	}else{
		values.vehicle_online_rate=0.0;
	}
	
	if(values.vehicle_online_count>0){
		values.vehicle_alarm_rate=parseFloat(((values.alarm_vehicle_count/values.vehicle_online_count)* 100).toFixed(2));
	}else{
		values.vehicle_alarm_rate=0.0;
	}
	

	if(values.total_time>0){
		values.online_rate=parseFloat(((values.online_time/values.total_time)* 100).toFixed(2));
	}else{
		values.online_rate=0.0;
	}
	values.trade_obj=trade_obj;
    values.v_id=[];
    values.v_has_net_id=[];
    values.v_has_track_id=[];
    values.v_alarm_id=[];
	values.v_location_id=[];
	values.v_exception_id=[];
	values.enterprise_ids=[];
	values.trade_kind_id=[];
	values.has_net=[];
	values.has_track=[];
	values.has_alarm=[];
	values.alarm_count_list=[];
    values.alarm_processed_list=[];
    values.overspeed_count_list=[];
    values.overspeed_processed_list=[];
    values.tired_count_list=[];
    values.tired_processed_list=[];
	return values;
}
 
db.runCommand({
    mapReduce: 'platform_swap',
    map: platform_aggregate_map,
    reduce: platform_aggregate_reduce,
	finalize: platform_aggregate_finalize,
    out: {
        replace: 'platform_swap_aggregate_temp_19'
    },
    sort: {
        _id: 1
    },query: {
        analyse_date : {$gte:ISODate("2017-09-18T16:00:00Z")} 
    }
});